﻿using HttpApiGateway.Model;

using RpcClient;

using RpcHelper;

using Wedonek.RpcStore.Gateway.Interface;
using Wedonek.RpcStore.Gateway.Model;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;
namespace Wedonek.RpcStore.Gateway.Service
{
        internal class SysConfigService : ISysConfigService
        {
                private readonly ISysConfigCollect _Config = null;
                private readonly IServerTypeCollect _ServerType = null;
                private readonly IServerCollect _Server = null;
                public SysConfigService (ISysConfigCollect config, IServerTypeCollect type, IServerCollect server)
                {
                        this._ServerType = type;
                        this._Config = config;
                        this._Server = server;
                }

                public long Add (SysConfigAddParam add)
                {
                        if (add.ValueType == SysConfigValueType.JSON)
                        {
                                add.Value = Tools.CompressJson (add.Value);
                        }
                        return this._Config.Add (add);
                }

                public void Drop (long id)
                {
                        this._Config.Drop (id);
                }

                public SysConfigDatum Get (long id)
                {
                        return this._Config.Get (id);
                }

                public SysConfigInfo[] Query (PagingParam<QuerySysParam> query, out long count)
                {
                        SysConfigDatum[] configs = this._Config.Query (query.Param, query.ToBasicPaging (), out count);
                        if (configs.Length == 0)
                        {
                                return System.Array.Empty<SysConfigInfo>();
                        }
                        ServerType[] types = this._ServerType.Gets (configs.Convert (a => !a.SystemType.IsNull(), a => a.SystemType));
                        ServerConfigDatum[] servers = this._Server.Gets (configs.Convert (a => a.ServerId != 0, a => a.ServerId));
                        return configs.ConvertMap<SysConfigDatum, SysConfigInfo> ((a, b) =>
                         {
                                 if (a.ServerId != 0)
                                 {
                                         b.Range = string.Format ("限定节点({0})使用", servers.Find (c => c.Id == a.ServerId, c => c.ServerName));
                                 }
                                 else if (!a.SystemType.IsNull())
                                 {
                                         b.Range = string.Format ("限定节点类型({0})使用", types.Find (c => c.TypeVal == a.SystemType, c => c.SystemName));
                                 }
                                 else
                                 {
                                         b.Range = "限定集群内";
                                 }
                                 return b;
                         });
                }
                public void SetIsEnable (long id, bool isEnable)
                {
                        this._Config.SetIsEnable (id, isEnable);
                }
                public void Set (SysConfigSet config)
                {
                        this._Config.Set (config.Id, config.ConvertMap<SysConfigSet, SysConfigSetParam> ());
                }
        }
}
